package com.sgb.kjwl.view.ui.fragment.packers;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.SparseArray;
import android.util.SparseIntArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.android.material.tabs.TabLayout;
import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.AtPackersEngineeringTenderBinding;
import com.sgb.kjwl.model.entity.TenderListEntity;
import com.sgb.kjwl.model.entity.packers.PickResultEntity;
import com.sgb.kjwl.model.entity.transport.GoodsInfoTypeEntity3;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.activity.pick.TenderDetailsActivity3;
import com.sgb.kjwl.view.ui.adapter.packers.PackerHallBidingAdapter1;
import com.sgb.kjwl.view.widget.GoodsTypePopWindow4;
import com.sgb.kjwl.view.widget.PickPopWindow1;
import com.sgb.kjwl.view.widget.ThreeLevelAreaWindow;
import com.sgb.kjwl.view.widget.pick.PickChooseLeftDialog2;
import com.sgb.kjwl.viewmodel.packers.PackersEngineeringTenderModel;
import com.sgb.link.entity.PickFilterEntity;
import com.sgb.link.utils.PickCateType;
import com.sgb.zhaocai.utils.PickCopyUtils;
import com.swgk.core.base.model.entity.SelectPopEntity;
import com.swgk.core.customview.ScreenChildTextView;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.interfaces.PopClick;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.entity.BaseAreaEntity;
import com.swgk.core.util.PickSelectEntity;
import com.swgk.core.util.PickSelectTextEntity;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.view.weiget.TwoLevelPopWindow;
import com.swgk.core.xrecyclerview.XRecyclerView;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author : younghow
 * @date : 2021/12/3 11:29
 * description:
 */
public class BuilderSubpackageFragment extends AppLazyFragment implements PickChooseLeftDialog2.DialogCallBackImpl {

    PackersEngineeringTenderModel viewModel;
    private AtPackersEngineeringTenderBinding binding;
    private PackerHallBidingAdapter1 adapter;

    /**
     * 工程分类
     */
    private TwoLevelPopWindow<PickFilterEntity> popCategory;
    private PickResultEntity pickResultProject1;
    private PickResultEntity pickResultProject2;
    private PickResultEntity pickResultProject3;
    private String projectName = "";
    private String projectNo = "";
    /**
     * 概算金额
     */
    private String priceNo = "";
    /**
     * 报名结束时间
     */
    private String timeNo = "";
    /**
     * 招标方式
     */
    private PickPopWindow1 popTenderMethod;
    private PickResultEntity pickResultTenderMethod;
    /**
     * 所在区域
     */
    private ThreeLevelAreaWindow popCity;
    private PickResultEntity pickResultProvince;
    private PickResultEntity pickResultCity;
    private PickResultEntity pickResultArea;
    private String areaName = "";
    private String mAreaCode, mAreaName;
    private boolean isLoadMore;
    private List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>> workTypeList;
    private GoodsTypePopWindow4 carsTypePopWindow;
    private int typePosition = -1;
    private List<GoodsInfoTypeEntity3> carsTypeData;
    private SelectPopEntity selectPopEntity0;

    public static BuilderSubpackageFragment newInstance() {
        BuilderSubpackageFragment intent = new BuilderSubpackageFragment();
        Bundle bundle = new Bundle();
        intent.setArguments(bundle);
        return intent;
    }

    private void initFilterData(boolean isLoadMore) {
        this.isLoadMore = isLoadMore;
        viewModel.getNewList(pickResultProject1.getNo(), priceNo, timeNo, pickResultTenderMethod.getNo(),
                pickResultProvince.getNo(), pickResultCity.getNo(), isLoadMore);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.at_packers_engineering_tender, container, false);
        return binding.getRoot();
    }

    @Override
    protected void initView() {

        binding.setLifecycleOwner(this);
        viewModel = new ViewModelProvider(this).get(PackersEngineeringTenderModel.class);

        binding.tvMore.setOnClickListener(this);
        binding.ivEmploymentAll.setOnClickListener(this);

        setTextButton();
        binding.sct.onSetCallPop(new ScreenChildTextView.ICallPop() {
            @Override
            public void openPop(int position, TextView content, LinearLayout bg, SelectPopEntity entity) {
                if (binding.sct.getSize() == 3){
                    if (position == 0) {
                        showCategoryPop();
                    } else if (position == 1) {
                        showTenderMethodPopWindow();
                    } else if (position == 2) {
                        showCityPop();
                    }
                }else if (binding.sct.getSize() == 2){
                    if (position == 0) {
                        showTenderMethodPopWindow();
                    } else if (position == 1) {
                        showCityPop();
                    }
                }
            }
        });

        if (adapter == null) {
            adapter = new PackerHallBidingAdapter1(getActivity(), null);
        }

        DividerItemDecoration divider = new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL);
        divider.setDrawable(Objects.requireNonNull(ContextCompat.getDrawable(getContext(), R.drawable.shape_list_divide2)));
        binding.recyclerView.addItemDecoration(divider);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.recyclerView.setAdapter(adapter);
        binding.recyclerView.setLoadingMoreEnabled(true);
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                initFilterData(false);
            }

            @Override
            public void onLoadMore() {
                initFilterData(true);
            }
        });
        adapter.setOnItemClick(new AdapterOnItemClick<TenderListEntity>() {
            @Override
            public void onItemClick(TenderListEntity tenderListEntity, int position) {
                TenderDetailsActivity3.start(getActivity(), tenderListEntity.getId(), 0);
            }
        });
        adapter.onIChildClick(tenderListEntity -> TenderDetailsActivity3.start(getActivity(), tenderListEntity.getId(), 0));

        mAreaCode = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.AREA_CODE, "");
        mAreaName = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.AREA_NAME, "");

        pickResultProject1 = new PickResultEntity();
        pickResultProject2 = new PickResultEntity();
        pickResultProject3 = new PickResultEntity();
        pickResultTenderMethod = new PickResultEntity();
        pickResultProvince = new PickResultEntity();
        pickResultCity = new PickResultEntity();
        pickResultArea = new PickResultEntity();

        binding.rvPackerChangeFirstContentTitle.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                carsTypePopWindow.setDefItem(tab.getPosition());
                if (tab.getPosition() != 0) {
                    pickResultProject1.setNo(workTypeList.get(tab.getPosition()).getNo());
                    pickResultProject1.setName(workTypeList.get(tab.getPosition()).getName());
                } else {
                    pickResultProject1.setNo("");
                    pickResultProject1.setName("全部");
                }

                pickResultProject2.clear();
                pickResultProject3.clear();
                initFilterData(false);

                if (tab.getPosition() == 0) {
                    binding.sct.removeTextButton(selectPopEntity0);
                    return;
                }else {
                    binding.sct.addTextButton(selectPopEntity0,0);
                    binding.sct.contentSelect("工程分类",false,false,0);
                }

                List<PickFilterEntity<List<PickFilterEntity>>> data = new ArrayList<>(workTypeList.get(tab.getPosition()).getChildren());
                List<PickFilterEntity> oneLevelData = new ArrayList<>();
                List<List<PickFilterEntity>> twoLevelData = new ArrayList<>();
                for (PickFilterEntity<List<PickFilterEntity>> datum : data) {
                    oneLevelData.add(datum);
                    List<PickFilterEntity> oneLevel = new ArrayList<>(datum.getChildren() == null ? new ArrayList<>() : datum.getChildren());
                    twoLevelData.add(oneLevel);
                }

                popCategory = new TwoLevelPopWindow<>(getActivity());
                popCategory.setBgView(binding.grayLayout);
                popCategory.setPopClick(new PopClick<PickFilterEntity>() {
                    @Override
                    public void reset() {
                        pickResultProject2.clear();
                        pickResultProject3.clear();
                        projectNo = "";
                        binding.sct.contentSelect("工程分类",false,false,0);
                        initFilterData(false);
                    }

                    @Override
                    public void sure(int provincePosition, int cityPosition, PickFilterEntity provinceEntity, PickFilterEntity cityEntity) {
                        if (provinceEntity != null) {
                            pickResultProject2.setNo(provinceEntity.getNo());
                            pickResultProject2.setName(provinceEntity.getName());
                        } else {
                            pickResultProject2.setNo("");
                            pickResultProject2.setName("全部");
                        }

                        if (cityEntity != null) {
                            pickResultProject3.setNo(cityEntity.getNo());
                            pickResultProject3.setName(cityEntity.getName());
                        } else {
                            pickResultProject3.setNo("");
                            pickResultProject3.setName("全部");
                        }

                        if (TextUtils.equals("全部", pickResultProject3.getName())) {
                            if (TextUtils.equals("全部", pickResultProject2.getName())){
                                projectNo = "";
                                binding.sct.contentSelect("工程分类",false,false,0);
                            } else {
                                projectNo = pickResultProject2.getNo();
                                binding.sct.contentSelect(pickResultProject2.getName(), true, false, 0);
                            }
                        } else {
                            projectNo = pickResultProject3.getNo();
                            binding.sct.contentSelect(pickResultProject3.getName(),true,false,0);
                        }
                        initFilterData(false);
                    }
                });

                popCategory.setOnDismissListener(() -> binding.sct.contentSelect(!"工程分类".equals(binding.sct.getName(0)),false,0));
                popCategory.setLinkData(oneLevelData, twoLevelData);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        carsTypeData = new ArrayList<>();

        carsTypePopWindow = new GoodsTypePopWindow4(getActivity());
        carsTypePopWindow.setBgView(binding.grayLayout);
        carsTypePopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object type, Object key2, Object key3) {
                GoodsInfoTypeEntity3 entity = (GoodsInfoTypeEntity3) type;
                typePosition = (int) key2;
                pickResultProject1.setNo(entity.getNo());
                if (typePosition != 0){
                    binding.sct.addTextButton(selectPopEntity0,0);
                }
                binding.rvPackerChangeFirstContentTitle.postDelayed(() -> binding.rvPackerChangeFirstContentTitle.getTabAt(typePosition).select(), 100);
                initFilterData(false);
            }
        });
        carsTypePopWindow.setOnDismissListener(() -> binding.ivEmploymentAll.setImageResource(R.mipmap.btn_expand_team_employment));

        initLiveData();
    }

    private void initLiveData() {
        viewModel.loading.observe(getViewLifecycleOwner(), aBoolean -> DialogHelper.showProgressDialog(getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false));

        viewModel.subPackageList.observe(getViewLifecycleOwner(), tenderListEntities -> {
            if (isLoadMore) {
                binding.recyclerView.loadMoreComplete();
                if (tenderListEntities != null && tenderListEntities.size() > 0) {
                    adapter.addDatas(tenderListEntities);
                }
                binding.recyclerView.setNoMore(tenderListEntities.size() < 10);
            } else {
                binding.recyclerView.refreshComplete();
                if (tenderListEntities != null && tenderListEntities.size() > 0) {
                    binding.emptyView.setVisibility(View.GONE);
                    adapter.setDatas(tenderListEntities);
                    binding.recyclerView.smoothScrollToPosition(0);
                } else {
                    adapter.clearDatas();
                    binding.emptyView.setVisibility(View.VISIBLE);
                }
            }
        });

        viewModel.popList.observe(getViewLifecycleOwner(), pickFilterEntities -> {
            workTypeList = pickFilterEntities;
            for (PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>> pickFilterEntity : pickFilterEntities) {
                GoodsInfoTypeEntity3 info = new GoodsInfoTypeEntity3();
                info.setName(pickFilterEntity.getName());
                info.setNo(pickFilterEntity.getNo());
                carsTypeData.add(info);
                binding.rvPackerChangeFirstContentTitle.addTab(binding.rvPackerChangeFirstContentTitle.newTab().setText(pickFilterEntity.getName()));
            }
        });

        viewModel.tenderMethodList.observe(getViewLifecycleOwner(), pickSelectEntities -> {
            if (popTenderMethod == null) {
                popTenderMethod = new PickPopWindow1(getContext());
                popTenderMethod.setBgView(binding.grayLayout);
                popTenderMethod.setOnResultClick((PickPopWindow1.OnResultClick) (key1, key2, key3) -> {
                    pickResultTenderMethod.setNo((String) key1);
                    pickResultTenderMethod.setName((String) key2);
                    pickResultTenderMethod.setPosition((int) key3);
                    if (TextUtils.isEmpty((String) key2) || TextUtils.equals("全部", (String) key2)) {
                        binding.sct.contentSelect("招标方式",false,false,binding.sct.getSize() == 3 ? 1 : 0);
                    } else {
                        binding.sct.contentSelect((String) key2,true,false,binding.sct.getSize() == 3 ? 1 : 0);
                    }
                    initFilterData(false);
                });
                popTenderMethod.setOnDismissListener(() -> binding.sct.contentSelect(!"招标方式".equals(binding.sct.getName(binding.sct.getSize() == 3 ? 1 : 0)),false,binding.sct.getSize() == 3 ? 1 : 0));

                popTenderMethod.setData(pickSelectEntities);
            }
        });

        viewModel.cityList.observe(getViewLifecycleOwner(), new Observer<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>() {
            @Override
            public void onChanged(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> addressEntities) {
                if (popCity == null) {
                    popCity = new ThreeLevelAreaWindow(getActivity()) {
                        @Override
                        protected void provinceClick(BaseAreaEntity baseAreaEntity, int position) {
                            super.provinceClick(baseAreaEntity, position);
                            pickResultProvince.setNo(baseAreaEntity.getNo());
                            pickResultProvince.setName(baseAreaEntity.getSimpleName());
                            if ("全部".equals(baseAreaEntity.getSimpleName()) || pickResultProvince.getName().equals(baseAreaEntity.getSimpleName())) {
                                pickResultCity.setNo("");
                                pickResultCity.setName("");
                            }
                        }

                        @Override
                        protected void cityClick(BaseAreaEntity baseAreaEntity, int position) {
                            super.cityClick(baseAreaEntity, position);
                            pickResultCity.setNo(baseAreaEntity.getNo());
                            pickResultCity.setName(baseAreaEntity.getSimpleName());
                            if ("全部".equals(baseAreaEntity.getSimpleName()) || pickResultCity.getName().equals(baseAreaEntity.getSimpleName())) {
                                pickResultArea.setNo("");
                                pickResultArea.setName("");
                            }
                        }

                        @Override
                        protected void areaClick(BaseAreaEntity baseAreaEntity, int position) {
                            super.areaClick(baseAreaEntity, position);
                            pickResultArea.setNo(baseAreaEntity.getNo());
                            pickResultArea.setName(baseAreaEntity.getSimpleName());
                        }

                        @Override
                        protected void reset() {
                            super.reset();
                            pickResultProvince.clear();
                            pickResultCity.clear();
                            pickResultArea.clear();
                            binding.sct.contentSelect("全部",true,false,binding.sct.getSize() == 3 ? 2 : 1);
                            popCity.dismiss();
                            initFilterData(false);
                        }
                    };
                    popCity.setBgView(binding.grayLayout);
                    popCity.setProvinceSingle();
                    popCity.setCitySingle();
                    popCity.setAreaSingle();
                    popCity.setShowSimpleName(true);
                    pickResultProvince.setName(mAreaName);
                    popCity.setOnResultClick((key1, key2, key3) -> {
                        initFilterData(false);
                        if (!"全部".equals(pickResultArea.getName()) && !"".equals(pickResultArea.getName())) {
                            binding.sct.contentSelect(pickResultArea.getName(),true,false,binding.sct.getSize() == 3 ? 2 : 1);
                            return;
                        } else {
                            binding.sct.contentSelect(pickResultCity.getName(),true,false,binding.sct.getSize() == 3 ? 2 : 1);
                        }

                        if (!"全部".equals(pickResultCity.getName()) && !"".equals(pickResultCity.getName())) {
                            binding.sct.contentSelect(pickResultCity.getName(),true,false,binding.sct.getSize() == 3 ? 2 : 1);
                            return;
                        } else {
                            binding.sct.contentSelect(pickResultProvince.getName(),true,false,binding.sct.getSize() == 3 ? 2 : 1);
                        }

                        if (!"全部".equals(pickResultProvince.getName()) && !"".equals(pickResultProvince.getName())) {
                            binding.sct.contentSelect(pickResultProvince.getName(),true,false,binding.sct.getSize() == 3 ? 2 : 1);
                        } else {
                            binding.sct.contentSelect("全部",true,false,binding.sct.getSize() == 3 ? 2 : 1);
                        }
                    });
                    popCity.setOnDismissListener(() -> binding.sct.contentSelect(!"地区".equals(binding.sct.getName(binding.sct.getSize() == 3 ? 2 : 1)),false,binding.sct.getSize() == 3 ? 2 : 1));

                    popCity.setDataForAll(addressEntities, true, mAreaCode);
                }
            }
        });

        viewModel.buildTypeList.observe(getViewLifecycleOwner(), pickSelectEntities -> {
            categoryData.clear();
            categoryData.addAll(pickSelectEntities);
            if (categoryData.size() != 0) {
                categoryData.get(0).setCheck(true);
                List<PickSelectEntity> list = new ArrayList<>(categoryData);
                refreshDataInfo(PickCateType.TYPE_CATEGORY_0, list);
            }
        });

        viewModel.timeList.observe(getViewLifecycleOwner(), pickSelectEntities -> refreshDataInfo(PickCateType.TYPE_SIGN_TIME, pickSelectEntities));

        viewModel.priceList.observe(getViewLifecycleOwner(), pickSelectEntities -> refreshDataInfo(PickCateType.TYPE_MONEY, pickSelectEntities));

        viewModel.tenderTypeList.observe(getViewLifecycleOwner(), pickSelectEntities -> refreshDataInfo(PickCateType.TYPE_SIGN, pickSelectEntities));

        viewModel.provinceList.observe(getViewLifecycleOwner(), pickSelectEntities -> refreshDataInfo(PickCateType.TYPE_PROVINCE, pickSelectEntities));

        viewModel.urlStr.observe(getViewLifecycleOwner(), s -> {
            String[] split = s.split("111");
            WebViewX5HostActivity.start(getActivity(), split[0], split[1]);
        });
    }

    @Override
    protected void onLazyLoad() {
        pickResultProvince.setNo(mAreaCode);
        binding.sct.contentSelect(mAreaName,true,false,2);
        initFilterData(false);

        initChooseDialog();

        viewModel.getCityList();
        viewModel.getTenderMethodList();
        viewModel.getTenderGoodsCategoryAllList();
        viewModel.getProvinceData(mAreaCode);
        viewModel.getPriceData();
        viewModel.getPickTimes();
        viewModel.getTenderNewTypeList();
        viewModel.getTenderProjectList();
    }

    @Override
    public void onMultiClick(View v) {
        super.onMultiClick(v);
        int id = v.getId();
        if (id == R.id.tv_more) {
            mLeftDialog2.initCategory(2, false, false);
            mLeftDialog2.initData(mData, mFilters);
            mLeftDialog2.showDialog();
        } else if (id == R.id.iv_employment_all) {
            if (carsTypeData != null && carsTypeData.size() > 0) {
                carsTypePopWindow.setData(carsTypeData, typePosition);
                carsTypePopWindow.showPopwindow(binding.ivEmploymentAll, 0, 0);
            }
            binding.ivEmploymentAll.setImageResource(R.mipmap.btn_expand_up_gray);
        }
    }

    /**
     * 工程-工程分类
     *
     * @param
     */
    public void showCategoryPop() {
        binding.sct.contentSelect(false, true,0);
        popCategory.showPopwindow0(binding.sct);
    }

    /**
     * 招标方式
     *
     * @param
     */
    public void showTenderMethodPopWindow() {
        binding.sct.contentSelect(false, true,binding.sct.getSize() == 3 ? 1 : 0);
        popTenderMethod.showPopwindow0(binding.sct);
    }

    /**
     * 省市区pop
     *
     * @param
     */
    public void showCityPop() {
        binding.sct.contentSelect(false, true,binding.sct.getSize() == 3 ? 2 : 1);
        popCity.showPopwindow(binding.sct, 0, 0);
    }


    /**
     * 更多筛选
     */
    private PickChooseLeftDialog2 mLeftDialog2;

    private final List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>>> categoryData = new ArrayList<>();
    /**
     * 数据集
     */
    private SparseArray<PickSelectTextEntity> mData = new SparseArray<>();
    private SparseIntArray mFilters = new SparseIntArray();
    private final SparseArray<String> nameArray = new SparseArray<>();

    private void initChooseDialog() {
        nameArray.put(PickCateType.TYPE_CATEGORY_0, "类别");
        nameArray.put(PickCateType.TYPE_CATEGORY_1, "类别1");
        nameArray.put(PickCateType.TYPE_CATEGORY_2, "类别2");
        nameArray.put(PickCateType.TYPE_SIGN, "招标方式");
        nameArray.put(PickCateType.TYPE_MONEY, "概算金额");
        nameArray.put(PickCateType.TYPE_SIGN_TIME, "报名结束时间");
        nameArray.put(PickCateType.TYPE_PROVINCE, "省份");
        nameArray.put(PickCateType.TYPE_CITY, "市区");
        nameArray.put(PickCateType.TYPE_DISTANCE, "距离");
        mLeftDialog2 = new PickChooseLeftDialog2(getContext(), nameArray);
        mLeftDialog2.setCallBack(this);
    }

    /**
     * 添加数据到中数据聚合
     */
    private void refreshDataInfo(int sort, List<PickSelectEntity> children) {
        clearOtherDataInfo(sort);
        addEntity(sort, children);
    }

    @Override
    public void loadCategory(PickSelectTextEntity entity, int itemPos) {
        List<PickSelectEntity> children = new ArrayList<>();
        if (itemPos > 0) {
            switch (entity.getSort()) {
                case PickCateType.TYPE_CATEGORY_0:
                    if (categoryData.size() == 0) {
                        return;
                    }
                    List<PickSelectEntity<List<PickSelectEntity>>> cate1 = categoryData.get(itemPos).getChildren();
                    if (cate1 != null && cate1.size() > 0) {
                        children.addAll(cate1);
                    }
                    mLeftDialog2.clearOtherDataInfo(entity.getSort());
                    mLeftDialog2.addEntity(PickCateType.TYPE_CATEGORY_1, children);
                    mLeftDialog2.showDialog();
                    break;
                case PickCateType.TYPE_CATEGORY_1:
                    if (categoryData.size() == 0) {
                        return;
                    }
                    int firPos = mLeftDialog2.getItemPos(0);
                    List<PickSelectEntity> cate2 = categoryData.get(firPos).getChildren().get(itemPos).getChildren();
                    if (cate2 != null && cate2.size() > 0) {
                        children.addAll(cate2);
                    }
                    mLeftDialog2.clearOtherDataInfo(entity.getSort());
                    mLeftDialog2.addEntity(PickCateType.TYPE_CATEGORY_2, children);
                    mLeftDialog2.showDialog();
                    break;
                case PickCateType.TYPE_PROVINCE:
                    viewModel.getCityData(entity.getItemNo(itemPos));
                    break;
                default:
                    break;
            }
        } else {
            if (entity.getSort() == PickCateType.TYPE_CATEGORY_0 || entity.getSort() == PickCateType.TYPE_CATEGORY_1 || entity.getSort() == PickCateType.TYPE_PROVINCE) {
                mLeftDialog2.clearOtherDataInfo(entity.getSort());
                mLeftDialog2.showDialog();
            }
        }
    }

    @Override
    public void loadData(SparseArray<PickSelectTextEntity> data, SparseIntArray map) {
        mData = PickCopyUtils.deepCopy(data);
        mFilters = map.clone();
        for (int i = 0; i < mData.size(); i++) {
            int key = mData.keyAt(i);
            PickSelectTextEntity entity = mData.get(key);
            updateChooseStatus(entity.getSort(), mFilters.get(entity.getSort()));
        }
        initFilterData(false);
    }

    @Override
    public void reset() {
        mFilters.clear();
        projectNo = "";
        projectName = "";
        priceNo = "";
        timeNo = "";
        areaName = "";
        pickResultCity.clear();
        pickResultTenderMethod.clear();
        pickResultProvince.clear();
        pickResultProject1.clear();
        pickResultProject2.clear();
        pickResultProject3.clear();
        clearOtherDataInfo(PickCateType.TYPE_CATEGORY_0);
        clearOtherDataInfo(PickCateType.TYPE_PROVINCE);
        loadData(mData, mFilters);
        mLeftDialog2.initData(mData, mFilters);
        mLeftDialog2.dismiss();
    }

    @Override
    public void onCancel() {
        mLeftDialog2.initData(mData, mFilters);
    }

    /**
     * 清理关联项为空
     */
    private void clearOtherDataInfo(int sort) {
        switch (sort) {
            case PickCateType.TYPE_CATEGORY_0:
                addEntity(PickCateType.TYPE_CATEGORY_1, new ArrayList<>());
                addEntity(PickCateType.TYPE_CATEGORY_2, new ArrayList<>());
                break;
            case PickCateType.TYPE_CATEGORY_1:
                addEntity(PickCateType.TYPE_CATEGORY_2, new ArrayList<>());
                break;
            case PickCateType.TYPE_PROVINCE:
                addEntity(PickCateType.TYPE_CITY, new ArrayList<>());
                break;
            default:
                break;
        }
    }

    public void addEntity(int index, List<PickSelectEntity> list) {
        if (mData.get(index) != null) {
            if (list == null || list.size() == 0) {
                mData.get(index).setPickSelectEntityList(new ArrayList<>());
            } else {
                mData.get(index).setPickSelectEntityList(list);
            }
        } else {
            mData.put(index, new PickSelectTextEntity(index, nameArray.get(index), list));
        }
        mFilters.put(index, 0);
    }

    public void setCityData(List<PickSelectEntity> data) {
        mLeftDialog2.addEntity(PickCateType.TYPE_CITY, data);
        mLeftDialog2.showDialog();
    }

    private void updateChooseStatus(int key, int value) {
        mFilters.put(key, value);
        String no = mData.get(key).getItemNo(value);
        String name = mData.get(key).getItemName(value);
        switch (key) {
            case PickCateType.TYPE_CATEGORY_0:
                //类别
                if (TextUtils.isEmpty(no)) {
                    projectNo = no;
                    projectName = "工程分类";
                } else {
                    projectNo = no;
                    projectName = name;
                }

                pickResultProject1.setNo(no);
                break;
            case PickCateType.TYPE_CATEGORY_1:
            case PickCateType.TYPE_CATEGORY_2:
                if (!TextUtils.isEmpty(no)) {
                    projectNo = no;
                    projectName = name;
                }
                break;
            case PickCateType.TYPE_SIGN:
                // 报名方式
                pickResultTenderMethod.setName(name);
                pickResultTenderMethod.setNo(no);
                if (TextUtils.isEmpty(no)) {
                    binding.sct.contentSelect("招标方式",false, false,binding.sct.getSize() == 3 ? 1 : 0);
                    return;
                }
                binding.sct.contentSelect(name,true, false,binding.sct.getSize() == 3 ? 1 : 0);
                break;
            case PickCateType.TYPE_MONEY:
                // 概算金额
                priceNo = no;
                break;
            case PickCateType.TYPE_SIGN_TIME:
                // 报名结束时间
                timeNo = no;
                break;
            case PickCateType.TYPE_PROVINCE:
                // 省
                if (TextUtils.isEmpty(no)) {
                    pickResultProvince.setNo(no);
                    pickResultProvince.setName("所在地区");
                    areaName = "所在地区";
                } else {
                    pickResultProvince.setNo(no);
                    pickResultProvince.setName(name);
                    areaName = name;
                }
                break;
            case PickCateType.TYPE_CITY:
                // 城市
                if (!TextUtils.isEmpty(no)) {
                    pickResultCity.setNo(no);
                    pickResultCity.setName(name);
                    areaName = name;
                }
                break;
            default:
                break;
        }
        if (binding.sct.getSize() == 3) {
            if (TextUtils.isEmpty(projectNo) || TextUtils.equals("工程分类", projectName)) {
                binding.sct.contentSelect("工程分类", false, false, 0);
            } else {
                binding.sct.contentSelect(projectName, true, false, 0);
            }
        }

        if (TextUtils.isEmpty(pickResultProvince.getNo()) || TextUtils.equals("所在地区", pickResultProvince.getName())) {
            binding.sct.contentSelect("所在地区",false, false,binding.sct.getSize() == 3 ? 2 : 1);
        } else {
            binding.sct.contentSelect(areaName,true, false,binding.sct.getSize() == 3 ? 2 : 1);
        }
    }

    private void setTextButton(){
        selectPopEntity0 = new SelectPopEntity("工程专项", "0", false, true);
        SelectPopEntity selectPopEntity1 = new SelectPopEntity("招标方式", "1", false, true);
        SelectPopEntity selectPopEntity2 = new SelectPopEntity("地区", "2", false, true);
        binding.sct.addTextButton(selectPopEntity0);
        binding.sct.addTextButton(selectPopEntity1);
        binding.sct.addTextButton(selectPopEntity2);
    }
}
